package com.android.porttallymansystem.logic

import android.app.Activity
import android.widget.Toast
import com.amap.api.maps.model.LatLng
import com.android.porttallymansystem.PTSApplication
import com.android.porttallymansystem.logic.database.MsgDatabase
import com.android.porttallymansystem.logic.database.UserDatabase
import com.android.porttallymansystem.logic.database.WorkDatabase
import com.android.porttallymansystem.logic.model.*

object Repository {

    private val userDao = UserDatabase.getDatabase(PTSApplication.context).userDao()
    private val msgDao = MsgDatabase.getDatabase(PTSApplication.context).msgDao()
    private val workDao = WorkDatabase.getDatabase(PTSApplication.context).workDao()

    private val locationList = mutableListOf<LatLng>()

    private val systemMessageList = mutableListOf<SystemMessage>().apply {
        add(
            SystemMessage(
                "年终考核结果",
                "您的年终考核结果已出，请自行查看！祝您来年工作更上一层楼！",
                SystemMessage.MessageTime(2021, 12, 30, 16, 27)
            )
        )
        add(
            SystemMessage(
                "余家头一号线开通！",
                "让余家头学生们心心念念的一号线在不久前终于开通！这为余家头的同学们带来了极大的交通便利，" +
                        "我终于可以在自己的学校坐地铁回家啦！",
                SystemMessage.MessageTime(2021, 12, 26, 9, 31)
            )
        )
        add(
            SystemMessage(
                "元旦放假通知",
                "元旦放假：2022年1月1日--1月3日，共3天。在此提前预祝各位新年快乐，身体健康！",
                SystemMessage.MessageTime(2021, 12, 25, 12, 2)
            )
        )
        add(
            SystemMessage(
                "12月工资单明细",
                "您的12月工资已到账，请在您账户和短信中查询明细工资单，祝您工作顺利！",
                SystemMessage.MessageTime(2021, 12, 10, 16, 13)
            )
        )
        add(
            SystemMessage(
                "11月项目评分表",
                "11月各任务、项目具体考核结果现已公示，请自行查看，如有疑问，请尽快反馈！",
                SystemMessage.MessageTime(2021, 12, 3, 12, 0)
            )
        )
        add(
            SystemMessage(
                "组织集体观看电影《长津湖》",
                "为进一步加强党史和爱国主义教育，厚植爱国情怀、传承革命精神，深入推进党史学习教育走深走实，" +
                        "11月17日晚上，组织号召集体观看重大革命历史题材电影《长津湖》，重温那段战火纷飞的峥嵘岁月，" +
                        "感受志愿军战士英勇无畏、舍生忘死、保家卫国的英雄主义精神。",
                SystemMessage.MessageTime(2021, 11, 14, 19, 57)
            )
        )
    }

    fun finishWork(id: Long) {
        val work = workDao.loadWorkById(id)
        work.isFinished = true
        workDao.updateWork(work)
    }

    fun getWorkById(id: Long): Work {
        return workDao.loadWorkById(id)
    }

    fun getWork(workerId: Long): MutableList<Work> {
        val workList = workDao.loadWorkByWorkerId(workerId)
        workList.sortBy { it.isFinished }
        return workList
    }

    fun sendMsg(content: String, receiverId: Long) {
        msgDao.insertMsg(Msg(receiverId, Setting.user.value!!.id, content))
    }

    fun getMessage(userId: Long, receiverId: Long): ArrayList<Message> {
        val msgList1 = msgDao.loadMsg(userId, receiverId)
        val msgList2 = msgDao.loadMsg(receiverId, userId)

        val msgList = mutableListOf<Msg>().apply {
            for (msg in msgList1) {
                add(msg)
            }
            for (msg in msgList2) {
                add(msg)
            }
        }

        msgList.sortBy { it.id }

        return arrayListOf<Message>().apply {
            for (msg in msgList) {
                if (msg.receiverId == receiverId) {
                    add(Message(msg.content, Message.TYPE_RECEIVED))
                } else {
                    add(Message(msg.content, Message.TYPE_SENT))
                }
            }
        }
    }

    fun getUser(id: Long): User {
        return userDao.loadUserById(id)
    }

    fun getBriefMessage(): List<BriefMessage> {
        val users = userDao.loadUsers()
        return mutableListOf<BriefMessage>().apply {
            for (user in users) {
                if (user.id != Setting.user.value!!.id) {
                    add(BriefMessage(user.id, user.profile, user.name))
                }
            }
        }
    }

    fun getSystemMessage(): List<SystemMessage> {
        return systemMessageList
    }

    fun addLatLng(latLng: LatLng) {
        locationList.add(latLng)
    }

    fun getLatLngList(): MutableList<LatLng> {
        return locationList
    }

    fun loginCheckByPhone(activity: Activity, phone: String, password: String): User? {
        val user = userDao.loadUserByPhone(phone)
        user?.let {
            return if (user.password == password) {
                activity.runOnUiThread {
                    Setting.setUser(user)
                }
                user
            } else {
                activity.runOnUiThread {
                    Toast.makeText(activity, "密码错误", Toast.LENGTH_SHORT).show()
                }
                null
            }
        }
        activity.runOnUiThread {
            Toast.makeText(activity, "暂无用户信息，请联系管理员注册", Toast.LENGTH_SHORT).show()
        }
        return null
    }

    fun loginCheckByEmail(activity: Activity, email: String, password: String): User? {
        val user = userDao.loadUserByEmail(email)
        user?.let {
            return if (user.password == password) {
                activity.runOnUiThread {
                    Setting.setUser(user)
                }
                user
            } else {
                activity.runOnUiThread {
                    Toast.makeText(activity, "密码错误", Toast.LENGTH_SHORT).show()
                }
                null
            }
        }
        activity.runOnUiThread {
            Toast.makeText(activity, "暂无用户信息，请联系管理员注册", Toast.LENGTH_SHORT).show()
        }
        return null
    }
}